import request from '../utls/require'

// 交换相关接口

/**
 * 获取交换物品列表
 */
export const getExchangeItems = (params?: any) => {
  console.log('[API] [Exchange] 开始发送获取交换物品列表请求');
  return request({
    url: '/xiangsu/exchange/items/',
    method: 'get',
    params
  }).then(response => {
    console.log('[API] [Exchange] 获取交换物品列表响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Exchange] 获取交换物品列表请求失败:', error);
    throw error;
  });
}

/**
 * 发布交换物品
 */
export const createExchangeItem = (data: any) => {
  console.log('[API] [Exchange] 开始发送发布交换物品请求');
  return request({
    url: '/xiangsu/exchange/items/',
    method: 'post',
    data
  }).then(response => {
    console.log('[API] [Exchange] 发布交换物品响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Exchange] 发布交换物品请求失败:', error);
    throw error;
  });
}

/**
 * 获取物品详情
 */
export const getExchangeItemDetail = (id: number) => {
  console.log(`[API] [Exchange] 开始发送获取物品${id}详情请求`);
  return request({
    url: `/xiangsu/exchange/items/${id}/`,
    method: 'get'
  }).then(response => {
    console.log(`[API] [Exchange] 获取物品${id}详情响应成功:`, response);
    return response;
  }).catch(error => {
    console.error(`[API] [Exchange] 获取物品${id}详情请求失败:`, error);
    throw error;
  });
}

/**
 * 删除物品
 */
export const deleteExchangeItem = (id: number) => {
  console.log(`[API] [Exchange] 开始发送删除物品${id}请求`);
  return request({
    url: `/xiangsu/exchange/items/${id}/`,
    method: 'delete'
  }).then(response => {
    console.log(`[API] [Exchange] 删除物品${id}响应成功:`, response);
    return response;
  }).catch(error => {
    console.error(`[API] [Exchange] 删除物品${id}请求失败:`, error);
    throw error;
  });
}

/**
 * 更新物品状态
 */
export const updateExchangeItemStatus = (id: number, status: string) => {
  console.log(`[API] [Exchange] 开始发送更新物品${id}状态请求: status=${status}`);
  return request({
    url: `/xiangsu/exchange/items/${id}/status/`,
    method: 'patch',
    data: { status }
  }).then(response => {
    console.log(`[API] [Exchange] 更新物品${id}状态响应成功:`, response);
    return response;
  }).catch(error => {
    console.error(`[API] [Exchange] 更新物品${id}状态请求失败:`, error);
    throw error;
  });
}

/**
 * 申请交换
 */
export const applyForExchange = (data: any) => {
  console.log('[API] [Exchange] 开始发送申请交换请求');
  return request({
    url: '/xiangsu/exchange/transactions/',
    method: 'post',
    data
  }).then(response => {
    console.log('[API] [Exchange] 申请交换响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Exchange] 申请交换请求失败:', error);
    throw error;
  });
}

/**
 * 获取我的物品列表
 */
export const getMyExchangeItems = (params?: any) => {
  console.log('[API] [Exchange] 开始发送获取我的物品列表请求');
  return request({
    url: '/xiangsu/exchange/my-items/',
    method: 'get',
    params
  }).then(response => {
    console.log('[API] [Exchange] 获取我的物品列表响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Exchange] 获取我的物品列表请求失败:', error);
    throw error;
  });
}

/**
 * 上传交换物品图片
 */
export const uploadExchangeImage = (data: FormData) => {
  console.log('[API] [Exchange] 开始发送上传交换物品图片请求');
  return request({
    url: '/xiangsu/exchange/upload/image/',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(response => {
    console.log('[API] [Exchange] 上传交换物品图片响应成功:', response);
    return response;
  }).catch(error => {
    console.error('[API] [Exchange] 上传交换物品图片请求失败:', error);
    throw error;
  });
}